<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class dangtin extends CI_Controller 
{
	var $account ;
	
	function __construct()
	{
		parent::__construct();
		$this->load->model('dangtin_model','dangtin');
		$this->load->helper('date');
		$this->load->helper('app');
		$this->account = isset($_SESSION['ses_login']) ? $_SESSION['ses_login']:'';
		if(empty($this->account)) redirect('') ;
	}
	
	function index()
	{
		if(isset($_POST['bt_submit']))
		{
			$this->form_validation->set_rules('lb_subject','Tiêu đề','trim|required');
			$this->form_validation->set_rules('lb_content','Nội dung','trim|required');
			$this->form_validation->set_rules('class_id','Danh mục','trim|required');
			if($this->form_validation->run()== FALSE){
				$this->pre_message = validation_errors();
			}else{
				$req = $this->_get_form();
				if($this->dangtin->save($req)){
					redirect('dangtin/raovat');
				}
	  		}
			redirect('dangtin') ;
		}
		
		$data['title'] = "Đăng tin rao vặt" ;
		$data['list_class'] = get_list_class() ;
		$data['list_town'] = get_list_town() ; 
		$data['page'] = 'dangtin' ;
		$this->load->view('html/skin',$data) ;
	}
	
	function raovat()
	{
		$data['title'] = "Đăng tin rao vặt" ;
		
		$data['start'] = (int) $this->uri->segment(4) ; 
		$config['base_url'] = base_url().'dangtin/raovat/'.(int)$this->uri->segment(3);  
		$data['num'] = $this->dangtin->count_all();
		$config['total_rows'] = $data['num'];
		$config['per_page'] = 20;
		$config['uri_segment'] = 4;
		$config['num_links'] = 5 ;
		$this->pagination->initialize($config);   
		$data['pagination'] = $this->pagination->create_links();
		
		$data['list'] = $this->dangtin->get_all($config['per_page'], $data['start']) ; 
		$this->site->load('raovat_user', $data, 'raovat') ;
	}
	
	function edit($id=0)
	{
		if(isset($_POST['bt_submit']))
		{
			$this->form_validation->set_rules('lb_subject','Tiêu đề','trim|required');
			$this->form_validation->set_rules('lb_content','Nội dung','trim|required');
			$this->form_validation->set_rules('class_id','Danh mục','trim|required');
			if($this->form_validation->run()== FALSE){
				$this->pre_message = validation_errors();
			}else{
				$req = $this->_get_form(1);
				if($this->dangtin->save($req, $id)){
					redirect('dangtin/raovat');
				}
	  		}
			redirect('dangtin') ;
		}
		$data['rs'] = $this->dangtin->get_id($id); 

		$data['list_town'] = get_list_town() ; 
		$data['list_class'] = get_list_class() ;
		$this->site->load("editraovat",$data, 'raovat');
	}
	
	private function _get_form($flag=0)
	{
		$req["lb_subject"]	= $this->input->post("lb_subject");
		$req["lb_content"]	= $this->input->post("lb_content");
		$req["class_id"]	= $this->input->post("class_id");
		$req["town_id"] 	= $this->input->post("town_id");
		$req["lb_image"] 	= $this->input->post("lb_image");
		$req["member_id"] 	= $this->account['member_id'] ;
		$req["dt_modify"] 	= date('Y-m-d H:i:s');
		if($flag) $req["dt_create"] 	= date('Y-m-d H:i:s');
		$req["bl_active"] 	= (int)$this->input->post("bl_active");
		
		return $req;
	}
}

?>